package com.lluohao.common;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lluohao.web.controller.MessageVO;
import com.lluohao.web.controller.Result;

/**
 * Created by Everthing-- on 2017/7/5.
 */
public class ChatMessageHandler implements MessageHandler {
    private static String url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=";
    public Result handler(MessageVO message) {
        String realUrl = url+message.getMsg();
        try {
            String resultStr = HttpUtil.get(realUrl);
            ObjectMapper mapper = new ObjectMapper();
            ChatResult result = mapper.readValue(resultStr, ChatResult.class);
            if(result.getResult()==0){
                Result realResult = new Result();
                realResult.setCode(200);
                realResult.setMessage(result.getContent());
                if(StringUtils.notNull(message.getGroupNum())){
                    realResult.setAt(message.getQq());
                }
                return realResult;
            }
        }catch (Exception e){
            return null;
        }
        return null;
    }
    public static void main(String[] args) {
        ChatMessageHandler handler = new ChatMessageHandler();
        MessageVO vo = new MessageVO();
        vo.setMsg("hello!");
        handler.handler(vo);
    }
}
